package xyz.chenyongpeng.orderconsumerdubbo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.chenyongpeng.entity.ZsGoodsImages;
import xyz.chenyongpeng.entity.ZsResource;
import xyz.chenyongpeng.entity.ZsUserAudioRelation;
import xyz.chenyongpeng.orderconsumerdubbo.vo.RelationSeriesVo;
import xyz.chenyongpeng.orderconsumerdubbo.vo.ResourceVo;
import xyz.chenyongpeng.service.*;
import xyz.chenyongpeng.to.GAudioTo;
import xyz.chenyongpeng.to.GoodsTicketTo;
import xyz.chenyongpeng.to.GoodsTo;
import xyz.chenyongpeng.to.NearbyResourceTo;
import xyz.chenyongpeng.utils.ImgUrl;

import java.util.*;

@Service
public class ResourceService {

    @Autowired
    private IZsResourceService zsResourceService;

    @Autowired
    private IZsGoodsImagesService zsGoodsImagesService;

    @Autowired
    private IZsNearbyResourcesService zsNearbyResourcesService;

    @Autowired
    private IZsGTicketsService zsGTicketsService;

    @Autowired
    private IZsGAudioService zsGAudioService;

    @Autowired
    private IZsUserAudioRelationService zsUserAudioRelationService;

    @Autowired
    private IZsGoodsService zsGoodsService;

    public ResourceVo getOne(String r_id){
        ZsResource zsResource = zsResourceService.getOne(new QueryWrapper<ZsResource>().eq("r_id", r_id));
        if (zsResource != null){
            ResourceVo resourceVo = new ResourceVo();
            resourceVo.setR_id(zsResource.getRId());
            resourceVo.setR_title(zsResource.getRTitle());
            resourceVo.setR_thumbnail(ImgUrl.setFullUrl(zsResource.getRThumbnail()));
//            TODO 需要重新修改info里图片路径
            resourceVo.setR_info(zsResource.getRInfo());
            resourceVo.setR_assist_info(zsResource.getRAssistInfo());
            resourceVo.setIs_need_pay(zsResource.getIsNeedPay());
            resourceVo.setIs_need_yy(zsResource.getIsNeedYy());
            resourceVo.setIs_have_audio(zsResource.getIsHaveAudio());
            resourceVo.setIs_sign_in(zsResource.getIsSignIn());
            return resourceVo;
        }
        return null;
    }

    /**
     * 获取轮播图
     * @param r_id
     * @return
     */
    public List<String> getImgListByLimit(String r_id){
        List<ZsGoodsImages> ZsGoodsImages = zsGoodsImagesService.list(new QueryWrapper<ZsGoodsImages>().eq("r_id", r_id));
        List<String> imgs = new ArrayList<>();
        if (ZsGoodsImages.size()>0){
            for (ZsGoodsImages img: ZsGoodsImages) {
                String src = img.getImgUrl();
                imgs.add(ImgUrl.setFullUrl(src));
            }
        }
        return imgs;
    }

    /**
     * 获取附近景点
     * @param r_id
     * @return
     */
    public List<NearbyResourceTo> getNearbyResources(String r_id){
        List<NearbyResourceTo> zsNearbyResources = zsNearbyResourcesService.getRelevantList(r_id);
        return zsNearbyResources;
    }

    /**
     * 获取门票
     * @param r_id
     * @return
     */
    public List<GoodsTicketTo> getAllTickets(String r_id){
        Map<String,Object> parms = new HashMap<>();
        parms.put("r_id", r_id);
        return zsGTicketsService.getAllTickets(parms);
    }

    /**
     * 获取音频
     * @param r_id
     * @return
     */
    public List<GAudioTo> getAllAudio(String r_id){
        Map<String,Object> parms = new HashMap<>();
        parms.put("r_id", r_id);
        return zsGAudioService.getAllAudio(parms);
    }

    public List<GAudioTo> checkHave(List<GAudioTo> list,String u_id){
        List<ZsUserAudioRelation> userAudioRelations = zsUserAudioRelationService.list(new QueryWrapper<ZsUserAudioRelation>().eq("u_id", u_id));
        if (userAudioRelations != null){
            for (ZsUserAudioRelation audio:userAudioRelations) {
                int good_id = audio.getGoodsId();
                for (GAudioTo gAudio:list) {
                    if (good_id == Integer.parseInt(gAudio.getGoods_id())){
                        gAudio.setIs_have(1);
                    }else{
                        gAudio.setIs_have(0);
                    }
                }
            }
        }
        return list;
    }

    public List<RelationSeriesVo> getRelationSeries(String relation_series_ids){
        Map<String,Object> parms = new HashMap<>();
        parms.put("goods_id", Arrays.asList(relation_series_ids.split(",")));
        parms.put("limit", "0,15");
        List<GoodsTo> goodsTos = zsGoodsService.getListByLimit(parms);
        List<RelationSeriesVo> relationSeries = new ArrayList<>();
        for (GoodsTo goods:goodsTos) {
            RelationSeriesVo relationSeriesVo = new RelationSeriesVo();
            relationSeriesVo.setGoods_id(goods.getGoods_id()+"");
            relationSeriesVo.setGoods_title(goods.getGoods_title());
            relationSeriesVo.setGoods_info(goods.getGoods_info());
            relationSeriesVo.setGoods_img(goods.getGoods_img());
            relationSeriesVo.setPrice(goods.getPrice());
            relationSeriesVo.setDiscount_amount(goods.getDiscount_amount());
            relationSeriesVo.setGt_id(goods.getGt_id()+"");
            relationSeriesVo.setR_id(goods.getR_id()+"");
            relationSeriesVo.setIs_appointment(goods.getIs_appointment()+"");
            relationSeries.add(relationSeriesVo);
        }
        return relationSeries;
    }

    public List<RelationSeriesVo> getRecommendSeries(String recommend_series_ids){
        Map<String,Object> parms = new HashMap<>();
        parms.put("goods_id", Arrays.asList(recommend_series_ids.split(",")));
        parms.put("limit", "0,15");
        List<GoodsTo> goodsTos = zsGoodsService.getListByLimit(parms);
        List<RelationSeriesVo> relationSeries = new ArrayList<>();
        for (GoodsTo goods:goodsTos) {
            RelationSeriesVo relationSeriesVo = new RelationSeriesVo();
            relationSeriesVo.setGoods_id(goods.getGoods_id()+"");
            relationSeriesVo.setGoods_title(goods.getGoods_title());
            relationSeriesVo.setGoods_info(goods.getGoods_info());
            relationSeriesVo.setGoods_img(goods.getGoods_img());
            relationSeriesVo.setPrice(goods.getPrice());
            relationSeriesVo.setDiscount_amount(goods.getDiscount_amount());
            relationSeriesVo.setGt_id(goods.getGt_id()+"");
            relationSeriesVo.setR_id(goods.getR_id()+"");
            relationSeriesVo.setIs_appointment(goods.getIs_appointment()+"");
            relationSeries.add(relationSeriesVo);
        }
        return relationSeries;
    }
}
